package com.fhz.lifeandvm;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.fhz.lifeandvm.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    ActivityMainBinding mBinding;

    private MainViewModel mMainViewModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        Log.i(TAG, "onCreate: ");
//        User user = new User();
//        getLifecycle().addObserver(user);

        //正确创建viewmodel  / new是不行.
        mMainViewModel = new ViewModelProvider(this,new MainViewModelFactory(3)).get(MainViewModel.class);

        mBinding.num.setText(String.valueOf(mMainViewModel.getNum()));


        mMainViewModel.list.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Log.i(TAG, "onChanged: "+s);
            }
        });

        mMainViewModel.number.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                mBinding.num.setText(String.valueOf(integer));
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        Log.i(TAG, "onDestroy: ");

    }

    public void click(View view) {

//        Integer integer = Integer.valueOf(mBinding.num.getText().toString());
//        mBinding.num.setText(String.valueOf(integer+1));
        mMainViewModel.add();
//        mBinding.num.setText(String.valueOf(mMainViewModel.getNum()));


        //setV
//        mMainViewModel.test();
    }
}